package com.sugar.real.estate.module.business.dal.mysql.apartment;

import cn.hutool.core.util.StrUtil;
import com.sugar.real.estate.framework.common.pojo.PageResult;
import com.sugar.real.estate.framework.mybatis.core.mapper.BaseMapperX;
import com.sugar.real.estate.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.sugar.real.estate.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.sugar.real.estate.module.business.controller.admin.apartment.vo.ApartmentPageReqVO;
import com.sugar.real.estate.module.business.controller.app.apartment.vo.AppApartmentPageReqVO;
import com.sugar.real.estate.module.business.controller.app.apartment.vo.AppApartmentSimpleRespVO;
import com.sugar.real.estate.module.business.dal.dataobject.apartment.ApartmentDO;
import com.sugar.real.estate.module.business.dal.dataobject.apartment.ApartmentHouseTypeDO;
import org.apache.ibatis.annotations.Mapper;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * 楼盘/公寓 Mapper
 *
 * @author Ysugar
 */
@Mapper
public interface ApartmentMapper extends BaseMapperX<ApartmentDO> {

    default PageResult<ApartmentDO> selectPage(ApartmentPageReqVO reqVO) {
        String name = reqVO.getNameZhCn();
        String projectProgress = reqVO.getProjectProgressZhCn();
        String builder = reqVO.getBuilderZhCn();
        String designer = reqVO.getDesignerZhCn();
        Long[] provinceIds = reqVO.getProvinceId();
        Long provinceId = null;
        Long cityId = null;
        Long countyId = null;
        if (Objects.nonNull(provinceIds)) {
            int length = provinceIds.length;
            switch (length){
                case 3:
                    countyId = provinceIds[2];
                case 2:
                    cityId = provinceIds[1];
                case 1:
                    provinceId = provinceIds[0];
            }
        }

        return selectPage(reqVO, new LambdaQueryWrapperX<ApartmentDO>()
                .eqIfPresent(ApartmentDO::getIsHot, reqVO.getIsHot())
                .eqIfPresent(ApartmentDO::getIsLatest, reqVO.getIsLatest())
                //.eqIfPresent(ApartmentDO::getMainHouseType, reqVO.getMainHouseType())
                .eqIfPresent(ApartmentDO::getPriceMin, reqVO.getPriceMin())
                .eqIfPresent(ApartmentDO::getPriceMax, reqVO.getPriceMax())
                .eqIfPresent(ApartmentDO::getProjectType, reqVO.getProjectType())
                .betweenIfPresent(ApartmentDO::getCompletionTime, reqVO.getCompletionTime())
                .eqIfPresent(ApartmentDO::getProvinceId, provinceId)
                .eqIfPresent(ApartmentDO::getCityId, cityId)
                .eqIfPresent(ApartmentDO::getCountyId, countyId)
                .betweenIfPresent(ApartmentDO::getCreateTime, reqVO.getCreateTime())
                .nested(StrUtil.isNotBlank(name),
                        o -> o.like(ApartmentDO::getNameZhCn, name)
                                .or().like(ApartmentDO::getNameEnUs, name))
                .nested(StrUtil.isNotBlank(projectProgress),
                        o -> o.like(ApartmentDO::getProjectProgressZhCn, projectProgress)
                                .or().like(ApartmentDO::getProjectProgressEnUs, projectProgress))
                .nested(StrUtil.isNotBlank(builder),
                        o -> o.like(ApartmentDO::getBuilderZhCn, builder)
                                .or().like(ApartmentDO::getBuilderEnUs, builder))
                .nested(StrUtil.isNotBlank(designer),
                        o -> o.like(ApartmentDO::getDesignerZhCn, designer)
                                .or().like(ApartmentDO::getDesignerEnUs, designer))
                .orderByDesc(ApartmentDO::getId));
    }

    default PageResult<AppApartmentSimpleRespVO> appGetApartmentPage(AppApartmentPageReqVO reqVO){
        String name = reqVO.getName();
        String projectProgress = reqVO.getProjectProgress();
        String price = reqVO.getPrice();
        BigDecimal priceMin = null;
        BigDecimal priceMax = null;
        if (StrUtil.isNotBlank(price)){
            String[] split = price.split("-");
            if (split.length == 2){
                String val1 = split[0].trim();
                if (StrUtil.isNotBlank(val1))
                    priceMin = new BigDecimal(val1);
                String val2 = split[1].trim();
                if (StrUtil.isNotBlank(val2))
                    priceMax = new BigDecimal(val2);
            }else if (split.length == 1){
                String val1 = split[0].trim();
                if (StrUtil.isNotBlank(val1))
                    priceMin = new BigDecimal(val1);
            }
        }

        String area = reqVO.getArea();
        BigDecimal areaMin = null;
        BigDecimal areaMax = null;
        if (StrUtil.isNotBlank(area)){
            String[] split = area.split("-");
            if (split.length == 2){
                String val1 = split[0].trim();
                if (StrUtil.isNotBlank(val1))
                    areaMin = new BigDecimal(val1);
                String val2 = split[1].trim();
                if (StrUtil.isNotBlank(val2))
                    areaMax = new BigDecimal(val2);
            }else if (split.length == 1){
                String val1 = split[0].trim();
                if (StrUtil.isNotBlank(val1))
                    areaMin = new BigDecimal(val1);
            }
        }

        BigDecimal finalPriceMin = priceMin;
        BigDecimal finalPriceMax = priceMax;

        String completionTimeStart = reqVO.getCompletionTimeStart();
        if (StrUtil.isBlank(completionTimeStart))
            completionTimeStart = null;
        String completionTimeEnd = reqVO.getCompletionTimeEnd();
        if (StrUtil.isBlank(completionTimeEnd))
            completionTimeEnd = null;

        return selectJoinPage(reqVO, AppApartmentSimpleRespVO.class, new MPJLambdaWrapperX<ApartmentDO>()
                .distinct()
                .selectAll(ApartmentDO.class)
                .leftJoin(ApartmentHouseTypeDO.class, ApartmentHouseTypeDO::getApartmentId, ApartmentDO::getId)
                .eqIfPresent(ApartmentHouseTypeDO::getHouseType, reqVO.getHouseType())
                .geIfPresent(ApartmentHouseTypeDO::getIndoorArea, areaMin)
                .leIfPresent(ApartmentHouseTypeDO::getIndoorArea, areaMax)
                .eqIfPresent(ApartmentDO::getProjectType, reqVO.getProjectType())
                .betweenIfPresent(ApartmentDO::getCompletionTime, completionTimeStart, completionTimeEnd)
                .eqIfPresent(ApartmentDO::getProvinceId, reqVO.getProvinceId())
                .eqIfPresent(ApartmentDO::getCityId, reqVO.getCityId())
                .eqIfPresent(ApartmentDO::getCountyId, reqVO.getCountyId())
                .eqIfPresent(ApartmentDO::getIsLatest, reqVO.getIsLatest())
                .eqIfPresent(ApartmentDO::getIsHot, reqVO.getIsHot())
                .eqIfPresent(ApartmentDO::getIsShow, 1)
                .nested(StrUtil.isNotBlank(name),
                        o -> o.like(ApartmentDO::getNameZhCn, name)
                                .or().like(ApartmentDO::getNameEnUs, name))
                .nested(Objects.nonNull(finalPriceMin),
                        o -> o.ge(ApartmentDO::getPriceMin, finalPriceMin).or()
                                .ge(ApartmentDO::getPriceMax, finalPriceMin))
                .nested(Objects.nonNull(finalPriceMax),
                        o -> o.le(ApartmentDO::getPriceMin, finalPriceMax).or()
                                .le(ApartmentDO::getPriceMax, finalPriceMax))
                .nested(StrUtil.isNotBlank(projectProgress),
                        o -> o.like(ApartmentDO::getProjectProgressZhCn, projectProgress)
                                .or().like(ApartmentDO::getProjectProgressEnUs, projectProgress))
                .orderByDesc(ApartmentDO::getId));
    }
}